﻿using Fdb.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

namespace Fdb
{
    /// <summary>
    /// 基础数据库上下文类
    /// </summary>
    public class BaseDbContext
    {
        /// <summary>
        /// 全局逻辑对象
        /// </summary>
        private readonly Logic<Object> Object = new Logic<Object>();
        public DatabaseType _DatabaseType = DatabaseType.Mysql;

        public BaseDbContext()
        {

        }

        /// <summary>
        /// 全局数据库上下文构造方法
        /// </summary>
        /// <param name="connectionString"></param>
        public BaseDbContext(string connectionString)
        {
            Object.ConnectionString = connectionString;
            var dbtype = ConfigurationManager.AppSettings["fyydbtype"];
            if (dbtype != null && dbtype.Equals("sqlite"))
            {
                _DatabaseType = DatabaseType.SQLite;
            }

            Object.DatabaseType = _DatabaseType;
        }

        /// <summary>
        /// 获取基础数据库逻辑对象
        /// </summary>
        /// <returns></returns>
        public Logic<Object> FetchBaseDbLogic()
        {
            return Object;
        }

        /// <summary>
        /// 获取数据库逻辑对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public Logic<T> FetchDbLogic<T>()
        {
            Logic<T> logic = new Logic<T>();
            logic.ConnectionString = Object.ConnectionString;
            logic.DatabaseType = Object.DatabaseType;
            return logic;
        }
    }
}
